package com.example.webgobang.config;

import com.example.webgobang.component.GameHandler;
import com.example.webgobang.component.MatchHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.annotation.Resource;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    private MatchHandler matchHandler;

    @Resource
    private GameHandler gameHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(matchHandler, "/findMatch") //收到 ws://127.0.0.1:8080/findMatch，调用matchHandler中的回调
                .addInterceptors(new HttpSessionHandshakeInterceptor()); //将HttpSession中的键值对，交付给WebSocketSession
        registry.addHandler(gameHandler, "/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
